home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / comms / netsoftware / amitcp2_x_gcc.lha / lineread.c < prev    next >
C/C++ Source or Header  |  1994-01-12  |  4KB  |  201 lines

  1. /*
  2.  * lineread.c
  3.  *
  4.  * Author: Tomi Ollila <too@cs.hut.fi>
  5.  *
  6.  * This module is FREEWARE. Standard "NO WARRANTY" disclaimer applies.
  7.  *
  8.  * Created: Thu Jul 12 16:40:03 EET 1990 too
  9.  * Last modified: Tue Jul 13 18:05:49 1993 too
  10.  *
  11.  * $Id: lineread.c,v 1.2 1994/01/12 18:23:14 jasegler Exp jasegler $
  12.  *
  13.  * HISTORY
  14.  * $Log: lineread.c,v $
  15.  * Revision 1.2  1994/01/12  18:23:14  jasegler
  16.  * *** empty log message ***
  17.  *
  18.  * Revision 1.1  1994/01/11  19:02:22  jasegler
  19.  * Initial revision
  20.  *
  21.  * Revision 1.1  1994/01/11  19:02:22  jasegler
  22.  * Initial revision
  23.  *
  24.  * Revision 1.3  1993/07/13  15:15:53  too
  25.  * Made lineread.c compile without <sys/cdefs.h> file
  26.  *
  27.  * Revision 1.1  1993/06/16  16:43:52  too
  28.  * Initial revision
  29.  *
  30.  *
  31.  */
  32.  
  33. #include "lineread.h"
  34.  
  35. #ifdef AMIGA
  36. extern struct Library *SocketBase;
  37. #define READ(a, b, c) recv(a, b, c, 0)
  38. #if __SASC
  39. #include <proto/socket.h>
  40. #elif __GNUC__
  41. #include <clib/socket_protos.h>
  42. #endif
  43. #else /* not AMIGA */
  44. #define READ(a, b, c) read(a, b, c)
  45. #endif /* AMIGA */
  46.  
  47. #ifndef NULL
  48. #define NULL 0
  49. #endif
  50.  
  51. #ifndef FALSE
  52. #define FALSE 0
  53. #endif
  54.  
  55. #ifndef TRUE
  56. #define TRUE 1
  57. #endif
  58.  
  59. #ifndef __CONCAT
  60. #if defined (__STDC__) || defined (__cplusplus)
  61. #define __CONCAT(x,y) x ## y
  62. #else
  63. #define __CONCAT(x,y) x/**/y
  64. #endif
  65. #endif /* __CONCAT not defined */
  66.  
  67. #define RLP(field) __CONCAT(rl->rl_Private.rlp_,field)
  68.  
  69.  
  70. #if defined (__STDC__) || defined (__cplusplus)
  71.  
  72. int
  73. lineRead (struct LineRead *rl)
  74.  
  75. #else
  76.  
  77. int
  78. lineRead (rl)
  79.      struct LineRead *rl;
  80.  
  81. #endif
  82. {
  83.   int i;
  84.  
  85.   if (RLP (Bufpointer) == RLP (Howlong))
  86.  
  87.     if (RLP (Selected))
  88.       {
  89.  
  90.     if (RLP (Line_completed))
  91.       RLP (Startp) = RLP (Bufpointer) = 0;
  92.  
  93.     if ((i = READ (rl->rl_Fd,
  94.                RLP (Buffer) + RLP (Bufpointer),
  95.                RLP (Buffersize) - RLP (Bufpointer))) <= 0)
  96.       {
  97.         /*
  98.            * here if end-of-file or on error. set Howlong == Bufpointer
  99.            * so if non-blocking I/O is in use next call will go to READ()
  100.          */
  101.         RLP (Howlong) = RLP (Bufpointer);
  102.         rl->rl_Line = NULL;
  103.         return i;
  104.       }
  105.     else
  106.       RLP (Howlong) = RLP (Bufpointer) + i;
  107.       }
  108.     else
  109.       /* Inform user that next call may block (unless select()ed) */
  110.       {
  111.     RLP (Selected) = TRUE;
  112.     return 0;
  113.       }
  114.   else
  115.     /* Bufpointer has not reached Howlong yet. */
  116.     {
  117.       RLP (Buffer)[RLP (Bufpointer)] = RLP (Saved);
  118.       RLP (Startp) = RLP (Bufpointer);
  119.     }
  120.  
  121.   /*
  122.    * Scan read string for next newline.
  123.    */
  124.   while (RLP (Bufpointer) < RLP (Howlong))
  125.     if (RLP (Buffer)[RLP (Bufpointer)++] == '\n')
  126.       goto Skip;
  127.  
  128.   /*
  129.    * Here if Bufpointer == Howlong.
  130.    */
  131.   if (rl->rl_Lftype != RL_LFNOTREQ)
  132.     {
  133.       RLP (Selected) = TRUE;
  134.  
  135.       if (RLP (Bufpointer) == RLP (Buffersize))
  136.     {
  137.       /*
  138.          * Here if Bufpointer reaches end-of-buffer.
  139.        */
  140.       if (RLP (Startp) == 0)
  141.         {            /* (buffer too short for whole string) */
  142.           RLP (Line_completed) = TRUE;
  143.           rl->rl_Line = RLP (Buffer);
  144.           RLP (Buffer)[RLP (Bufpointer)] = '\0';
  145.           return -1;
  146.         }
  147.       /*
  148.          * Copy partial string to start-of-buffer and make control ready for
  149.          * filling rest of buffer when next call to lineRead() is made
  150.          * (perhaps after select()).
  151.        */
  152.       for (i = 0; i < RLP (Buffersize) - RLP (Startp); i++)
  153.         RLP (Buffer)[i] = RLP (Buffer)[RLP (Startp) + i];
  154.       RLP (Howlong) -= RLP (Startp);
  155.       RLP (Bufpointer) = RLP (Howlong);
  156.       RLP (Startp) = 0;
  157.     }
  158.  
  159.       RLP (Line_completed) = FALSE;
  160.       return 0;
  161.     }
  162.  
  163. Skip:
  164.   RLP (Line_completed) = TRUE;
  165.   if (rl->rl_Lftype == RL_LFREQNUL)
  166.     RLP (Buffer)[RLP (Bufpointer) - 1] = '\0';
  167.   RLP (Saved) = RLP (Buffer)[RLP (Bufpointer)];
  168.   RLP (Buffer)[RLP (Bufpointer)] = '\0';
  169.   RLP (Selected) = FALSE;
  170.   rl->rl_Line = RLP (Buffer) + RLP (Startp);
  171.  
  172.   return (RLP (Bufpointer) - RLP (Startp));
  173. }
  174.  
  175. #undef READ
  176.  
  177. #if defined (__STDC__) || defined (__cplusplus)
  178.  
  179. void
  180. initLineRead (struct LineRead *rl, int fd, int lftype, int buffersize)
  181.  
  182. #else
  183.  
  184. int
  185. initLineRead (rl, fd, lftype, buffersize)
  186.      struct LineRead *rl;
  187.      int fd;
  188.      int lftype;
  189.      int buffersize;
  190.  
  191. #endif
  192. {
  193.   rl->rl_Fd = fd;
  194.   rl->rl_Lftype = lftype;
  195.  
  196.   RLP (Bufpointer) = RLP (Howlong) = 0;
  197.   RLP (Selected) = RLP (Line_completed) = TRUE;
  198.  
  199.   RLP (Buffersize) = buffersize;
  200. }
  201.